package ch.zhaw.cctd.gui;

import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Der Startdialog des Spiels. Wird das Programm gestartet, erscheint dieser
 * Dialog, der den Spieler dazu auffordert, einen Namen anzugeben. Danach wird
 * man zum Spielmenü geführt. Von dort kann man ein Spiel erstellen oder einem
 * beitreten.
 * 
 * 
 * @author nathanaelkoch
 * 
 */
public class StartGUI implements ActionListener, KeyListener {
	private CCTDFrame frame;

	private JTextField nameField;
	private JButton nameButton = new JButton("Anmelden");
	private JLabel nameError = new JLabel();

	private JButton createGame = new JButton("Spiel erstellen");
	private JButton joinGame = new JButton("Spiel beitreten");

	private JTextField joinField;
	private JButton joinButton = new JButton("Beitreten");
	private JLabel joinError = new JLabel();
	private JButton toGametype = new JButton("Zurück");

	public StartGUI(CCTDFrame frame) {
		this.frame = frame;
	}

	public void start() {
		if (frame.isSetUsername())
			choseGametype();
		else
			login();
	}

	private void login() {
		JPanel c = new JPanel();
		c.setName("Mothership");
		c.removeAll();
		c.setLayout(new GridBagLayout());
		JPanel loginPanel = new JPanel(new FlowLayout());
		loginPanel.setName("DecoratedPanel");
		JLabel nameLabel = new JLabel("Username: ");
		String[] exampleUsernames = { "For the horde!", "Die in my name!", "Getsuga Tensho!", "What do?", "Brothafromanothamotha",
				"Troll is successful!", "Laser Cat", "m00t", "Leeroy Jenkins", "Holy Cow!" };
		nameField = new JTextField(exampleUsernames[new Random().nextInt(exampleUsernames.length)], 15);
		nameField.addKeyListener(this);
		nameButton.addActionListener(this);

		loginPanel.add(nameLabel);
		loginPanel.add(nameField);
		loginPanel.add(nameButton);
		loginPanel.add(nameError);
		c.add(loginPanel);

		frame.setContentPane(c);
	}

	private void checkUsername() {
		if (nameField.getText().equals("")) {
			nameError.setText("Sie müssen einen Namen angeben!");
		} else {
			frame.setUsername(nameField.getText());
			choseGametype();
		}
	}

	private void choseGametype() {
		JPanel c = (JPanel) frame.getContentPane();
		c.removeAll();
		c.setLayout(new GridBagLayout());

		createGame.addActionListener(this);
		joinGame.addActionListener(this);

		JPanel p = new JPanel(new GridLayout(2, 1));
		p.setName("DecoratedPanel");
		p.add(createGame);
		p.add(joinGame);

		c.add(p);
		frame.setContentPane(c);
	}

	private void joinGame() {
		JPanel c = (JPanel) frame.getContentPane();
		c.removeAll();
		c.setLayout(new GridBagLayout());

		joinField = new JTextField(15);
		JLabel joinLabel = new JLabel("IP-Adresse: ");

		joinField.addActionListener(this);
		joinField.addKeyListener(this);
		joinButton.addActionListener(this);
		toGametype.addActionListener(this);

		JPanel p1 = new JPanel(new GridLayout(1, 3));
		p1.add(joinLabel);
		p1.add(joinField);
		p1.add(joinButton);

		JPanel p2 = new JPanel(new GridLayout(3, 1));
		p2.setName("DecoratedPanel");
		p2.add(p1);
		p2.add(joinError);
		p2.add(toGametype);

		c.add(p2);

		frame.setContentPane(c);
	}

	private void checkAddress() {
		String address = joinField.getText();

		frame.joinGame(address);
	}

	@SuppressWarnings("unused")
	private boolean checkIP(String address) {
		return true;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(nameButton)) {
			checkUsername();
		} else if (e.getSource().equals(joinGame)) {
			joinGame();
		} else if (e.getSource().equals(createGame)) {
			frame.createGame();
		} else if (e.getSource().equals(joinButton)) {
			checkAddress();
		} else if (e.getSource().equals(toGametype)) {
			choseGametype();
		}
	}

	public void keyReleased(KeyEvent e) {
		if (e.getSource().equals(nameField) && e.getKeyCode() == KeyEvent.VK_ENTER) {
			checkUsername();
		} else if (e.getSource().equals(nameField)) {
			if(nameField.getText().length() > CCTDFrame.NAME_LENGTH) {
				nameField.setText(nameField.getText().substring(0,CCTDFrame.NAME_LENGTH));
			}
		} else if (e.getSource().equals(joinField) && e.getKeyCode() == KeyEvent.VK_ENTER) {
			checkAddress();
		}
	}

	public void keyPressed(KeyEvent e) {
	}

	public void keyTyped(KeyEvent e) {
	}

}
